#!/usr/bin/python

def escape(m):
	"""Convert a HTML entity into normal string (ISO-8859-1)"""
	if m.group(1)=='#':
		try:
			return chr(int(m.group(2)))
		except ValueError:
			return '&#%s;' % m.group(2)
	try:
		return htmlentitydefs.entitydefs[m.group(2)]
	except KeyError:
		return '&%s;' % m.group(2)

def unescape(s):
	"""Convert a HTML quoted string into normal string (ISO-8859-1). 
	
	Works with &#XX; and with &nbsp; &gt; etc."""
	return re.sub(r'&(#?)(.+?);',convertentity,s)
